package com.vm.action.system;

import java.sql.Timestamp;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.opensymphony.xwork2.Preparable;
import com.vm.action.ActionSuper;
import com.vm.common.JsonUtile;
import com.vm.common.MD5;
import com.vm.common.Page;
import com.vm.dao.system.IUser;
import com.vm.entity.User;
import com.vm.log4j.CustomizeLog4j;

public class UserAction extends ActionSuper implements Preparable {
	private static Logger _log = CustomizeLog4j.getLogger(UserAction.class);
	private IUser userDAO;
	private Page<User> page;
	private String pageLinkList;
	private User user;
	private List<Map<String, Object>> listMap;
	private String jsonTree;
	private Map roleMap;
	private String[] roleIdValues;
	private String newPwd;
	private Map<String,String> roleSelectMap;
	
	private String sarchSignName;
	private String sarchName;
	private String sarchOrgNo;
	
	private String passwordMassege;
	
	public void prepare() throws Exception {
		super.setButtonGrpMap(userDAO.seletButtonGrp((User)this.getSession().get("user"), "userAction"));
	}

	public void initial(){
		listMap = userDAO.findAllAgency();
		jsonTree = JsonUtile.mapToJson(listMap);
		String hql = "select modulPrmssCd as id, rlNm as name from Role";
		roleMap = userDAO.getCheckBoxMap(hql);
	}
	
	public String list() {
		//page = this.userDAO.pageForOrderByIdDesc(PAGE_ROW_SIZE, super.getPageNumber());
		String hql = "from User where (1=1)";
		String url = "/user/userAction_list.do?";
		List<String> wherList = new ArrayList<String>();
		if(sarchSignName != null && !"".equals(sarchSignName)){
			   hql += " and signNm like ?";	
			   url +="&sarchSignName="+sarchSignName;
			   wherList.add("%"+ sarchSignName+"%");
		}
		if(sarchName != null && !"".equals(sarchName)){
			   hql += " and userName like ?";
			   url +="&sarchName="+sarchName;
			   wherList.add("%"+sarchName+"%");
		}
		if(sarchOrgNo != null && !"".equals(sarchOrgNo)){
			   hql += " and blngOrgNo=?";	
			   url +="&sarchOrgNo="+sarchOrgNo;
			   wherList.add(sarchOrgNo);
		}
		
		page = this.userDAO.page(hql, "select count(*)"+hql, wherList.toArray(), PAGE_ROW_SIZE, super.getPageNumber());
		this.pageLinkList = super.getSplitPageLink(url, page);
		//select orgNo as id,suprOrgNo as  pId,orgNm as name, arGrp as arGrp FROM Agency
		roleSelectMap =userDAO.getSelectPoMap("select orgNo as id,orgNm as name FROM Agency");
		return SUCCESS;
	}

	public String add() throws Exception {
		this.initial();
		if(user == null){
			user = new User();
			user.setVldMrk(1);
		}
		return SUCCESS;
	}

	public String save() throws Exception {
		boolean flag = this.userDAO.validator(user.getSignNm());
		if (flag) {
			this.initial();
			this.addFieldError("userErr", user.getSignNm()+ super.getText("check_adminuser_name"));
			return "input";
		}
		String[] msgArg = new String[] { "user/userAction_list.do", "user/userAction_add.do" };
		String msg = null;
		try {
			String pwd = MD5.encode(user.getUserPw());
			Timestamp time = new Timestamp(System.currentTimeMillis());
			user.setUserPw(pwd);
			user.setRlGrp(userDAO.getButtonValues(roleIdValues));
			user.setRnwTm(time);
			user.setLtstSignTm(time);
			if(user.getBrwsOrgNo() != null && !"".equals(user.getBrwsOrgNo())){
				user.setBrwsOrgNo(userDAO.getButtonValues(user.getBrwsOrgNo().split(",")));
			}
			this.userDAO.save(user);
			msg = this.getResourceMessage("insert_adminuser_success", msgArg);
		} catch (Exception e) {
			e.printStackTrace();
			msg = this.getResourceMessage("insert_adminuser_err", msgArg);
		}
		super.setShowMessage(msg);
		return super.SHOW_MESSAGE_RESULT;
	}

	public String edit() throws Exception {
		user = userDAO.findById(user.getUserId());
		roleIdValues = userDAO.getGrpValues(user.getRlGrp());
		this.initial();
		return SUCCESS;
	}

	public String updatePassword(){
		return SUCCESS;
	}
	
	public String resetPassword(){
		if(((User)this.getSession().get("user")).isSuperAdmin()){
			user = userDAO.findById(user.getUserId());
			user.setUserPw(MD5.encode("123456"));
			userDAO.update(user);
			passwordMassege ="用户"+user.getSignNm()+"的密码已设置为初始密码123456请及时修改!";
		}else{
			passwordMassege ="重置用户密码失败!";
		}
		return this.list();
	}
	public String updatePwd(){
		
		String[] msgArg = new String[] {"admin/user/userAction_updatePassword.do"};
		String msg = null;
		try {
			String pwd = MD5.encode(user.getUserPw());
			user =(User) this.getSession().get("user");
			if(user.getUserPw().equals(pwd)){
				user.setUserPw(MD5.encode(newPwd));
				userDAO.update(user);
				msg = this.getResourceMessage("update_updateOld_success", msgArg);
			}else{
				msg = this.getResourceMessage("update_oldPws_erre", msgArg);
			}
		} catch (Exception e) {
			e.printStackTrace();
			msg = this.getResourceMessage("update_updateOldms_err", msgArg);
		}
		super.setShowMessage(msg);
		return super.SHOW_MESSAGE_RESULT;
	}
	public String update() throws Exception {

		String[] msgArg = new String[] { "admin/user/userAction_list.do" };
		String msg = null;
		try {
			user.setRnwTm(new Timestamp(System.currentTimeMillis()));
			user.setRlGrp(userDAO.getButtonValues(roleIdValues));
			user.setBrwsOrgNo(userDAO.getButtonValues(user.getBrwsOrgNo().split(",")));
			userDAO.update(user);
			msg = this.getResourceMessage("update_adminuser_success", msgArg);
		} catch (Exception e) {
			e.printStackTrace();
			msg = this.getResourceMessage("update_adminuser_err", msgArg);
		}

		super.setShowMessage(msg);
		return super.SHOW_MESSAGE_RESULT;

	}

	/**
	 * 系统默认的管理员不能删除
	 * **/
	public String delete() {
		String[] msgArg = new String[] { "admin/user/userAction_list.do" };
		String msg = null;
		try {
			this.userDAO.deleteById(user.getUserId());
			msg = this.getResourceMessage("delete_success_link", msgArg);
		} catch (Exception e) {
			msg = this.getResourceMessage("delete_err_link", msgArg);
		}

		super.setShowMessage(msg);
		return super.SHOW_MESSAGE_RESULT;
	}
	
	public String view() {
		initial();
		user = userDAO.findById(user.getUserId());
		roleIdValues = userDAO.getGrpValues(user.getRlGrp());
		return SUCCESS;
	}

	public IUser getUserDAO() {
		return userDAO;
	}

	public void setUserDAO(IUser userDAO) {
		this.userDAO = userDAO;
	}

	public Page<User> getPage() {
		return page;
	}

	public void setPage(Page<User> page) {
		this.page = page;
	}

	public String getPageLinkList() {
		return pageLinkList;
	}

	public void setPageLinkList(String pageLinkList) {
		this.pageLinkList = pageLinkList;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public List<Map<String, Object>> getListMap() {
		return listMap;
	}

	public void setListMap(List<Map<String, Object>> listMap) {
		this.listMap = listMap;
	}

	public String getJsonTree() {
		return jsonTree;
	}

	public void setJsonTree(String jsonTree) {
		this.jsonTree = jsonTree;
	}

	public Map getRoleMap() {
		return roleMap;
	}

	public void setRoleMap(Map roleMap) {
		this.roleMap = roleMap;
	}

	public String[] getRoleIdValues() {
		return roleIdValues;
	}

	public void setRoleIdValues(String[] roleIdValues) {
		this.roleIdValues = roleIdValues;
	}

	public String getNewPwd() {
		return newPwd;
	}

	public void setNewPwd(String newPwd) {
		this.newPwd = newPwd;
	}

	public Map<String, String> getRoleSelectMap() {
		return roleSelectMap;
	}

	public void setRoleSelectMap(Map<String, String> roleSelectMap) {
		this.roleSelectMap = roleSelectMap;
	}

	public String getSarchSignName() {
		return sarchSignName;
	}

	public void setSarchSignName(String sarchSignName) {
		this.sarchSignName = sarchSignName;
	}

	public String getSarchName() {
		return sarchName;
	}

	public void setSarchName(String sarchName) {
		this.sarchName = sarchName;
	}

	public String getSarchOrgNo() {
		return sarchOrgNo;
	}

	public void setSarchOrgNo(String sarchOrgNo) {
		this.sarchOrgNo = sarchOrgNo;
	}

	public String getPasswordMassege() {
		return passwordMassege;
	}

	public void setPasswordMassege(String passwordMassege) {
		this.passwordMassege = passwordMassege;
	}
	
}
